package org.test.once.manager;

import java.sql.SQLException;
import java.util.List;

import org.test.once.dao.UserDao;
import org.test.once.model.User;

/**
 * 
 * @author cennanfang
 * @date 2013-7-22
 * @fileName UserManager.java
 */
public class UserManager {
	/**
	 * 用户登录
	 * 
	 * @param account
	 * @param password
	 * @return
	 */
	public User login(String account, String password) {
		User user = null;
		try {
			user = UserDao.getInstance().findUserByAccount(account);
			if (!(user != null && user.getPassword().equals(password))) {
				user = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 获取用户列表
	 * 
	 * @return
	 */
	public List<User> searchUsers() {
		List<User> users = null;
		try {
			users = UserDao.getInstance().searchUsers();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return users;
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 */
	public void addUser(User user) {
		try {
			UserDao.getInstance().save(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过Id删除用户
	 * 
	 * @param id
	 */
	public void delUser(int id) {
		try {
			UserDao.getInstance().deleteUser(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过Id查找用户
	 * 
	 * @param id
	 */
	public User findUserById(int id) {
		User user = null;
		try {
			user = UserDao.getInstance().findUserById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 更新用户
	 * 
	 * @param user
	 */
	public void updateUser(User user) {
		try {
			UserDao.getInstance().updateUser(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void updateUserRole(int role_id, int id) {
		UserDao.getInstance().updateUserRole(role_id, id);
	}
	
	public static void main(String[] args) {
		new UserManager().searchUsers();
	}

}
